package fr.hoteia.appengine.archetype.server.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.cache.Cache;
import javax.cache.CacheEntry;
import javax.cache.CacheException;

import com.google.appengine.api.memcache.stdimpl.GCacheFactory;

import fr.hoteia.appengine.archetype.client.Constants;
import fr.hoteia.appengine.archetype.server.service.PortalManager;

/**
 * <p>
 * <a href="PortalManagerImpl.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author Denis GOSSET <a href="http://www.hoteia.com"><i>Hoteia.com</i></a>
 * 
 */
public class PortalManagerImpl extends BaseManager implements PortalManager {

	@SuppressWarnings("unchecked")
	public Cache getCacheTypePortal() {
		Cache cache = getCache(Constants.PORTAL_CACHE);
		if (cache == null) {
			try {
				Map props = new HashMap();
				props.put(GCacheFactory.EXPIRATION_DELTA, Constants.PORTAL_CACHE_EXPIRATION_DELTA);
				cache = getCacheFactory().createCache(props);
			} catch (CacheException e) {
				e.printStackTrace();
			}
		}
		return cache;
	}

	public String getParamInCache(String paramName) {
		Cache cache = getCacheTypePortal();
		String paramValue = null;
		CacheEntry cacheEntry = cache.getCacheEntry(paramName);
		if (cacheEntry != null) {
			paramValue = (String) cacheEntry.getValue();
		}
		return paramValue;
	}

	@SuppressWarnings("unchecked")
	public void setParamInCache(String paramName, String paramValue) {
		getCacheTypePortal().put(paramName, paramValue);
	}
	
	public Object getObjectInCache(String objectName) {
		Cache cache = getCacheTypePortal();
		Object objectValue = null;
		CacheEntry cacheEntry = cache.getCacheEntry(objectName);
		if (cacheEntry != null) {
			objectValue = (Object) cacheEntry.getValue();
		}
		return objectValue;
	}

	@SuppressWarnings("unchecked")
	public void setObjectInCache(String objectName, Object objectValue) {
		getCacheTypePortal().put(objectName, objectValue);
	}
}
